package com.yonyou.brigade.order_center.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.yonyou.brigade.order_center.dao.BriFlightPassengerMapper;
import com.yonyou.brigade.order_center.model.BriFlightInfo;
import com.yonyou.brigade.order_center.dao.BriFlightInfoMapper;
import com.yonyou.brigade.order_center.model.BriFlightInfoDTO;
import com.yonyou.brigade.order_center.model.BriFlightPassenger;
import com.yonyou.brigade.order_center.service.IBriFlightInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yonyou.brigade.order_center.util.OrderConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiangzhf@yonyou.com
 * @since 2018-07-09
 */
@Service
public class BriFlightInfoServiceImpl extends ServiceImpl<BriFlightInfoMapper, BriFlightInfo> implements IBriFlightInfoService {

    @Autowired
    private BriFlightInfoMapper briFlightInfoMapper;

    @Autowired
    private BriFlightPassengerMapper briFlightPassengerMapper;

    public  Map getBriFlightInfoDTOs(Map params)
    {
//        orgpk(组织pk)，pageSize(页面大小),current(当前页面),condition(过滤条件)
        Map queryParams = params;
        queryParams.put("tenant",params.get("tenantId"));
        int pageSize=Integer.parseInt(params.get("pageSize").toString());
        int current=Integer.parseInt(params.get("current").toString());
        if(pageSize<=0) {
            pageSize=10;
        }
        if(current<=0) {
            current=1;
        }
        int begin=(current-1)*pageSize;
        queryParams.put("begin",begin);
        queryParams.put("pageSize",pageSize);
        Map result=new HashMap();
        try {
            List<BriFlightInfoDTO> briFlightInfoDTOS= briFlightInfoMapper.getBriFlightInfoDTOs(queryParams);
            int total=briFlightInfoMapper.getBriFlightInfoDTOsCount(queryParams);
            // 展示时将订单总金额平均分配
            if (!CollectionUtils.isEmpty(briFlightInfoDTOS)) {
                for (BriFlightInfoDTO briFlightInfoDTO : briFlightInfoDTOS) {
                    String orderNo = briFlightInfoDTO.getOrderNo();
                    Integer flightInfoCount = briFlightInfoMapper.selectCount(new EntityWrapper<BriFlightInfo>().where("order_no={0}", orderNo));
                    Integer flightPassengerCount = briFlightPassengerMapper.selectCount(new EntityWrapper<BriFlightPassenger>().where("order_no={0}", orderNo));
                    if (flightPassengerCount > 1) {
                        briFlightInfoDTO.setPaymentPrice(String.valueOf(new BigDecimal(briFlightInfoDTO.getPaymentPrice()).divide(new BigDecimal(flightPassengerCount),4,BigDecimal.ROUND_HALF_UP)));
                        briFlightInfoDTO.setTpPrice(String.valueOf(new BigDecimal(briFlightInfoDTO.getTpPrice()).divide(new BigDecimal(flightPassengerCount),4,BigDecimal.ROUND_HALF_UP)));
                        briFlightInfoDTO.setSumCounterFee(String.valueOf(new BigDecimal(briFlightInfoDTO.getSumCounterFee()).divide(new BigDecimal(flightPassengerCount),4,BigDecimal.ROUND_HALF_UP)));
                        briFlightInfoDTO.setSumInsMoney(String.valueOf(new BigDecimal(briFlightInfoDTO.getSumInsMoney()).divide(new BigDecimal(flightPassengerCount),4,BigDecimal.ROUND_HALF_UP)));
                        briFlightInfoDTO.setChangeFee(String.valueOf(new BigDecimal(briFlightInfoDTO.getChangeFee()).divide(new BigDecimal(flightPassengerCount),4,BigDecimal.ROUND_HALF_UP)));
                        briFlightInfoDTO.setRefundPrice(String.valueOf(new BigDecimal(briFlightInfoDTO.getRefundPrice()).divide(new BigDecimal(flightPassengerCount),4,BigDecimal.ROUND_HALF_UP)));
                        briFlightInfoDTO.setOilfee(String.valueOf(new BigDecimal(briFlightInfoDTO.getOilfee()).divide(new BigDecimal(flightPassengerCount),4,BigDecimal.ROUND_HALF_UP)));
                        briFlightInfoDTO.setConstructionPrice(String.valueOf(new BigDecimal(briFlightInfoDTO.getConstructionPrice()).divide(new BigDecimal(flightPassengerCount),4,BigDecimal.ROUND_HALF_UP)));
                    }
                    if (flightInfoCount > 1) {
                        briFlightInfoDTO.setPaymentPrice(String.valueOf(new BigDecimal(briFlightInfoDTO.getPaymentPrice()).divide(new BigDecimal(flightInfoCount),4,BigDecimal.ROUND_HALF_UP)));
                        briFlightInfoDTO.setTpPrice(String.valueOf(new BigDecimal(briFlightInfoDTO.getTpPrice()).divide(new BigDecimal(flightInfoCount),4,BigDecimal.ROUND_HALF_UP)));
                        briFlightInfoDTO.setSumCounterFee(String.valueOf(new BigDecimal(briFlightInfoDTO.getSumCounterFee()).divide(new BigDecimal(flightInfoCount),4,BigDecimal.ROUND_HALF_UP)));
                        briFlightInfoDTO.setSumInsMoney(String.valueOf(new BigDecimal(briFlightInfoDTO.getSumInsMoney()).divide(new BigDecimal(flightInfoCount),4,BigDecimal.ROUND_HALF_UP)));
                        briFlightInfoDTO.setChangeFee(String.valueOf(new BigDecimal(briFlightInfoDTO.getChangeFee()).divide(new BigDecimal(flightInfoCount),4,BigDecimal.ROUND_HALF_UP)));
                        briFlightInfoDTO.setRefundPrice(String.valueOf(new BigDecimal(briFlightInfoDTO.getRefundPrice()).divide(new BigDecimal(flightInfoCount),4,BigDecimal.ROUND_HALF_UP)));
                        briFlightInfoDTO.setConsturctAndFuel(String.valueOf(new BigDecimal(briFlightInfoDTO.getConsturctAndFuel()).divide(new BigDecimal(flightInfoCount),4,BigDecimal.ROUND_HALF_UP)));
                        briFlightInfoDTO.setCounterFee(String.valueOf(new BigDecimal(briFlightInfoDTO.getCounterFee()).divide(new BigDecimal(flightInfoCount),4,BigDecimal.ROUND_HALF_UP)));
                        briFlightInfoDTO.setBindCounterFee(String.valueOf(new BigDecimal(briFlightInfoDTO.getBindCounterFee()).divide(new BigDecimal(flightInfoCount),4,BigDecimal.ROUND_HALF_UP)));
                    }
                }
            }
            List<BriFlightInfoDTO> briFlightInfoDTOList=perfectFlightInfoDTOs(briFlightInfoDTOS);
            result.put("information",briFlightInfoDTOList);
            result.put("code","0");
            JSONObject pagination=new JSONObject();
            pagination.put("total",total);
            pagination.put("pageSize",pageSize);
            pagination.put("current",current);
            result.put("pagination",pagination);
        }
        catch (Exception e)
        {
                result.put("code","100");
                result.put("information","操作失败");
        }


        return  result;

    }

    private List<BriFlightInfoDTO> perfectFlightInfoDTOs(List<BriFlightInfoDTO> flightInfoDTOS)
    {
        List<BriFlightInfoDTO> flightInfoDTOList=new ArrayList<>();
        if(flightInfoDTOS==null)
        {
            return  flightInfoDTOS;
        }
        else
        {
            for(int i=0;i<flightInfoDTOS.size();i++)
            {
                flightInfoDTOList.add(OrderConvert.perfactFlightDTO(flightInfoDTOS.get(i)));
            }
        }
        return  flightInfoDTOList;
    }

    //add duhqh
    public List<BriFlightInfo> getBriFlightInfo(String orderNo){
        return briFlightInfoMapper.getBriFlightInfoList(orderNo);
    }
}
